﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _13_ListView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //增加表头内容
            this.listView1.Columns.Add("日志等级", 120, HorizontalAlignment.Left);
            this.listView1.Columns.Add("时间", 200, HorizontalAlignment.Left);
            //设置消息列的宽度
            int infoWidth = this.listView1.Width - 120 - 200;
            this.listView1.Columns.Add("消息", infoWidth, HorizontalAlignment.Left);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PrintLog("这是一条:Debug消息", LogLevel.Debug);
        }

        public void PrintLog(string info,LogLevel logLevel)
        {
            //创建ListViewItem对象(列表使用空间中的项，专用于列表视图控件显示的数据)
            ListViewItem lvi = new ListViewItem();
            lvi.Text = logLevel.ToString();//显示日志等级
            lvi.ImageIndex = (int)logLevel; //设置图标索引

            //根据不同的等级设置不同的颜色
            switch (logLevel)
            {
                case LogLevel.Debug:
                    lvi.ForeColor = Color.SeaGreen;
                    break;
                case LogLevel.Info:
                    lvi.ForeColor = Color.Black;
                    break;
                case LogLevel.Error:
                    lvi.ForeColor = Color.Red;
                    break;
                case LogLevel.Warn:
                    lvi.ForeColor = Color.Orange;
                    break;
                case LogLevel.Fatal:
                    lvi.ForeColor = Color.Purple;
                    break;
            }

            //添加时间
            lvi.SubItems.Add(DateTime.Now.ToString());

            //添加消息
            lvi.SubItems.Add(info);

            //添加到控件中
            this.listView1.Items.Add(lvi);

            //确保最下方的内容可见，即滚动条自动下滑
            this.listView1.EnsureVisible(this.listView1.Items.Count - 1);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            PrintLog("这是一条:Info消息", LogLevel.Info);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            PrintLog("这是一条:Error消息", LogLevel.Error);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            PrintLog("这是一条:Warn消息", LogLevel.Warn);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            PrintLog("这是一条:Fatal消息", LogLevel.Fatal);
        }
    }
}
